package com.example.singleton.seriable;

import java.io.*;

/**
 */
public class SeriableSingleton implements Serializable {


    public  final static SeriableSingleton INSTANCE = new SeriableSingleton();
    private SeriableSingleton(){}

    public static SeriableSingleton getInstance(){
        return INSTANCE;
    }

    private Object readResolve() {
        return INSTANCE;
    }


    public static void main(String[] args) {
        SeriableSingleton s1 =null;
        SeriableSingleton s2 = SeriableSingleton.getInstance();

        try {
//            OutputStream out = new FileOutputStream("SeriableSingleton.obj");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(s2);
            oos.flush();
            oos.close();

//            InputStream in = new FileInputStream("SeriableSingleton.obj");
            InputStream in = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(in);
            s1 = (SeriableSingleton)ois.readObject();
            ois.close();

            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s1 == s2);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
